<template>
    <div>
        <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
                <span>基本信息</span>
                <div style="float: right;">
                    <el-button size="small" @click="onClose">取 消</el-button>
                    <el-button size="small" type="primary" @click="onFormSubmit">保存</el-button>
                </div>
            </div>
            <form-create v-model="form.fApi" :rule="form.rule" :option="form.option" @btn-off-flag-change="offFlagRaido" @btn-pdf-click="uploadPdf" @btn-project-click="projectOpen(true)"/>
            <input type="file" ref="pdfFile" @change="pdfChange" style="display: none;" />
        </el-card>

        <el-dialog :title="project.title" :visible.sync="project.visible" :width="project.width" :close-on-click-modal="false" append-to-body>
            <projectFormList 
            v-if="project.hackReset" 
            :tableData="project.tableData" 
            :tableLabel="project.tableLabel" 
            :models="project"
            @on-search-click="projectOpen"
            @on-check-click="projectCheckSelected"  
            @on-cancel-click="project.visible=false"/>
        </el-dialog>

        <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <span>采购劳务</span>
            </div>
            <el-table :data="material.tableData" style="width: 100%;" ref="table2" stripe>
                <el-table-column prop="name" label="劳务分类名称"></el-table-column>
                <el-table-column prop="applyNum" label="申请数量"></el-table-column>
                <el-table-column prop="estimatePrice" label="预估价">
                    <template slot-scope="scope">
                        {{ scope.row.estimatePrice/100}}
                    </template>
                </el-table-column>
                <el-table-column prop="purchasedNum" label="采购数量">
                    <template slot-scope="scope">
                        <el-input v-model='scope.row.purchasedNum' oninput="value=value.indexOf('.') > -1?value.slice(0, value.indexOf('.') + 3):value"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="skuInfo" label="备注" width="250">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.p5" type="textarea"></el-input>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        
  
           
    </div>
</template>

<script>
    import { 
        FORM_PURCHASE_OVERT,
        OPTION_INLINE,
        OPTION_BLOCK 
    } from "@/views/purchase/purchaseModel/purchase_form"
    import { 
        PARAMS_MATERIAL,
        LABEL_MATERIAL,
        LABEL_PORJECT
    } from "@/config/common"
    import projectFormList from "@/components/modules/project_form_list"
    import { uploads } from "@/api/common_api"
    import { getProjectList } from "@/api/project_api"
    import { bidPurchaseCreate} from "@/api/labour_purchase_api"
    export default {
        components:{
            projectFormList
        },
        props: ['row'],
        data() {
            return {
                form:{
                    fApi:{},
                    rule:FORM_PURCHASE_OVERT,
                    option:OPTION_BLOCK
                },
                //临时附件储存
                annxFileName:'',
                material:{
                    tableData:[],
                    tableLabel: LABEL_MATERIAL,
                    total:0
                },
                project:{
                    title:'项目库',
                    width:"1200px",
                    hackReset:true,
                    visible:false,
                    tableData:[],
                    tableLabel: LABEL_PORJECT,
                    total:0
                },
                params: {
                    type:"page",
                    pageNum: 1,
                    pageSize: 10
                }
            };
        },
        mounted(){
            this.initData();
            //console.log(process.env.VUE_APP_FILE)
        },
        methods: { 
            //初始化数据
            initData(){
                this.form.fApi.setValue({
                    "name":"",
                    "startTime":"",
                    "endTime":"",
                    "openTime":"",
                    "offFlag":"",
                    "receiverName":"",
                    "receiverPhone":"",
                    "payType":"",
                    "address":"",
                    "remarks":"",
                    "reason":"",
                    "files":"",
                });
                this.form.fApi.hidden(false,'openTime');
                this.form.fApi.updateValidates({'openTime': [{required:true}]})//默认开标时间显示
                this.annxFileName = '';//清空临时储存附件
                this.material.tableData = [];//物资
                this.project.tableData = [];//供应商
                //判断是单个还是多个物资
                if(Array.isArray(this.row)){
                    this.form.fApi.setValue({"name":this.row[0].name});//项目名称
                    this.material.tableData = this.row;
                }else{
                    this.form.fApi.setValue({"name":this.row.name});//项目名称
                    this.material.tableData = [this.row];
                }   
            },
            //选择项目库
            async projectOpen(formData){
                let params = {};
                if(formData === true){
                    params.pageNum = 1;
                    params.pageSize = 10;
                }else{
                    params.pageNum = formData.pageNum;
                    params.pageSize = formData.pageSize;
                    params.name = formData.name;
                }
                const {code,data} = await getProjectList(params);
                if(code === 200){
                    this.project.tableData = data.list;
                    this.project.total = data.total;
                    if(formData === true){//新增的时候才打开
                        this.project.visible = true;
                    }
                }
               
            },
            //提交项目库
            projectCheckSelected(row){
                this.form.fApi.setValue({"receiverName":row[0].receiverName,"receiverPhone":row[0].receiverPhone,"address":row[0].address});
                this.project.visible = false;
            },
            //上传pdf
            uploadPdf(){
                this.$refs.pdfFile.click();
            },
            async pdfChange(e){
                e.preventDefault();
                let _this = this;
                let file = e.target.files[0];
                if(file===undefined){return;}
                let formData = new FormData();
                formData.append("resType",file);
                const {code,data} = await uploads('annxPDF',formData);
                if(code === 200){
                    this.annxFileName = data;//临时储存
                    this.form.fApi.setValue({"files":file.name});//传给后端
                    e.target.value = "";
                }
            },
            //切换线上 不需要开标时间 线下需要
            offFlagRaido(val){
                if(val === '线上'){
                    this.form.fApi.hidden(true,'openTime');
                    this.form.fApi.updateValidates({'openTime': [{required:false}]})
                }else if(val === '线下'){
                    this.form.fApi.hidden(false,'openTime');
                    this.form.fApi.updateValidates({'openTime': [{required:true}]})
                }
            },
            //提交
            onFormSubmit(){
                this.form.fApi.submit(async (formData, $f) => {
                try {
                    //bidGoodsDetailVOList  采购物资明细
                    //dept 部门信息
                    //project 项目信息
                    const _material = this.material.tableData;
                    for(var i=0;i<_material.length;i++){
                        if(_material[i].purchasedNum === 0 || _material[i].purchasedNum === ''){
                            return this.$message.error('物资信息第'+(i+1)+'行采购数量未填写');
                        }
                        if(_material[i].purchasedNum > _material[i].applyNum){
                            return this.$message.error('物资信息第'+(i+1)+'行采购数量大于了申请数量');
                        }
                    }
                    let params = {};
                    params.bidLabourDetailVOList = this.material.tableData;//劳务列表
                    let totalSumAll = 0;
                    this.material.tableData.map((item) => {totalSumAll += item.purchasedNum*item.estimatePrice})
                    params.totalPrice = totalSumAll;//采购总价
                    params.address = formData.address;//地址
                    params.receiverPhone =formData.receiverPhone;//联系电话
                    params.receiverName = formData.receiverName;//联系人
                    params.files = this.annxFileName;//附件
                    params.name = formData.name;//项目名称
                    if(formData.offFlag === '1'){//如果线下就需要开标时间
                        params.openTime = formData.openTime;
                    }
                    params.offFlag = formData.offFlag;//评审状态
                    params.payType = formData.payType;//支付方式
                    params.bidType = 3;
                    params.projectType = 2;//招标内容 1：物资招标 2.劳务招标
                    params.startTime = formData.startTime;//申请时间
                    params.endTime = formData.endTime;//开标时间
                    params.reason = formData.reason;//其他原因
                    params.remarks = formData.remarks;//备注
                    const {code,msg} = await bidPurchaseCreate(params);
                    if(code === 200){
                        this.$message.success('操作成功');
                        this.onClose();
                    }
                } catch (error) {}
                })
            },
            onClose(){
                this.$emit('on-cancel-click','overt');
            }
        }
    };
    </script>
    
    <style>
        /*按钮*/
        .el-input__suffix{
            right: 0 !important;
        }
    </style>